package com.itcncp.wechat.app.rpc.service.impl;

import cn.jpush.api.JPushClient;
import com.itcncp.common.annotation.BaseService;
import com.itcncp.common.base.BaseServiceImpl;
import com.itcncp.common.util.Jpush;
import com.itcncp.wechat.app.common.constant.ApiConstant;
import com.itcncp.wechat.app.dao.mapper.UserCarInfoMapper;
import com.itcncp.wechat.app.dao.model.UserCarInfo;
import com.itcncp.wechat.app.dao.model.UserCarInfoExample;
import com.itcncp.wechat.app.rpc.api.UserCarInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

/**
* UserCarInfoService实现
* Created by itcncp on 2018/1/12.
*/
@Service
@Transactional
@BaseService
public class UserCarInfoServiceImpl extends BaseServiceImpl<UserCarInfoMapper, UserCarInfo, UserCarInfoExample> implements UserCarInfoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserCarInfoServiceImpl.class);

    @Autowired
    UserCarInfoMapper userCarInfoDao;

    /**
     * 用户添加车牌
     * @param userCarInfo
     * @return
     */
    @Override
    public UserCarInfo addUserCard(UserCarInfo userCarInfo) {

        //数据是否删除  1、删除  2、不删除
        userCarInfo.setDel(new Byte( "2" ));
        userCarInfo.setUpdateTime( new Date(  ) );
        userCarInfo.setCreateTime( new Date(  ) );
        //默认审核状态 车牌审核 1、未审核 2、审核通过 3、审核未通过
        userCarInfo.setIsCheck( new Byte( "1" ) );

        // 判断车牌是否存在
        UserCarInfo itemUserCard = userCarInfoDao.selectByPrimaryKey( userCarInfo.getUserId(),userCarInfo.getPlate() );
        if(null != itemUserCard){
            //车牌已存在 且未删除
            if(itemUserCard.getDel() == 2)
            {
                return  null;
            }else {
                //如果已删除，则重新激活绑定
                userCarInfoDao.updateByPrimaryKeySelective(userCarInfo);
            }
        }else {
            //添加车牌
            userCarInfoDao.insert( userCarInfo );
        }

        Jpush.jpushClient = new JPushClient( ApiConstant.JPUSH_ADMIN_MASTER_SECRET,ApiConstant.JPUSH_ADMIN_APPKEY);

        Jpush.allDevices("您有新的车牌审核信息：" + userCarInfo.getPlate() + ",请立即审核!",new HashMap<String, String>(  ));

        return userCarInfo;
    }

}